package etc.com.market;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import etc.com.sql.DBAdapter;

import Controls.JsonDataHelper;
import android.app.Application;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MarketApp<MyCount> extends ListActivity implements OnClickListener {

	// private ImageButton m_btnSchedule;
	// private ImageButton m_btnLetures;
	// private ImageButton m_btnExist;
	// private Resources m_res;
	// private MarketSystem app;
	// private TestConnection TC;
	public static MarketApp seft;
	private Handler mHandler = new Handler();
	ProgressDialog d;
	boolean isOldDBVersion = false;

	/** Called when the activity is first created. */
	@Override
	// main
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		Button btn = (Button) findViewById(R.id.button);

		btn.setOnClickListener(this);
		mHandler.post(changeScreen);
	}
	
	private Runnable changeScreen = new Runnable() {
		public void run() {
			try{
				Thread.sleep(1000);
				new changeScreenTask().execute(new String[0]);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	// Cac ham dinh nghia

	public static void CloseApp() {
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	public void onClick(View v) {
		if(mHandler != null){
			mHandler.removeCallbacks(changeScreen);
		}
		Intent intent = new Intent(this, StoreListAct.class);
		startActivity(intent);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public class changeScreenTask extends AsyncTask<String, Integer, Long>{

		@Override
		protected Long doInBackground(String... params) {
			Intent intent = new Intent(getApplicationContext(), StoreListAct.class);
			JSONArray jsonStoreArrObj = JsonDataHelper.getJsonObjectFromServer("http://superappli.wacontre.com/web/?target=store_list");
			JSONArray jsonInfoArr = JsonDataHelper.getJsonObjectFromServer("http://superappli.wacontre.com/web?target=info");
			if(jsonStoreArrObj.size() > 0 && jsonInfoArr.size() > 0){
				DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
				SQLiteDatabase db = dbAdapter.getDB();
				dbAdapter.onCreate(db);
				for(int i = 0; i < jsonStoreArrObj.size(); i++){
					JSONObject jsonObj = (JSONObject) jsonStoreArrObj.get(i);
					String storeid = jsonObj.get("id").toString();
					String storeName = jsonObj.get("STRNM").toString();
					String addressId = jsonObj.get("ADID").toString();
					String add = jsonObj.get("ADD").toString();
					String image = jsonObj.get("IMAGE").toString();
					dbAdapter.insertStoreTable(storeid, storeName, add, image, addressId);
				}
				
				for(int j = 0; j < jsonInfoArr.size(); j++){
					JSONObject jsonInfoObj = (JSONObject) jsonInfoArr.get(j);
					String id = jsonInfoObj.get("id").toString();
					String title = jsonInfoObj.get("INFOTITLE").toString();
					String detailTitle = jsonInfoObj.get("INFODLTTITLE").toString();
					String content = jsonInfoObj.get("DSCRPT").toString();
					dbAdapter.insertInfomation(id, title, detailTitle, content);
				}
				
				MarketSystem marketSystem = (MarketSystem) getApplication();
				marketSystem.setOldDBVersion(false);
			}else{
				MarketSystem marketSystem = (MarketSystem) getApplication();
				marketSystem.setOldDBVersion(true);
			}
			startActivity(intent);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
			return null;
		}
	}
}